﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/2/24 星期日 20:55:58
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections;
using Sector7.Framework.Core;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Reflection;
using Sector7.Framework.Consts;
using Microsoft.Win32;

namespace Sector7.Framework.Utility
{
    /// <summary>
    /// 客户端工具集
    /// </summary>
    public class ClientUtil
    {
        #region Public functions
        /// <summary>
        /// 加载绑定数据集合
        /// </summary>
        /// <param name="dataGrid">DataGrid</param>
        /// <param name="itemsSource">数据集合</param>
        public static void LoadItemsSource(DataGrid dataGrid, IEnumerable itemsSource)
        {
            dataGrid.IsReadOnly = false;
            dataGrid.ItemsSource = itemsSource;
            dataGrid.IsReadOnly = true;

            if (dataGrid.HasItems)
            {
                dataGrid.SelectedIndex = 0;
            }
            dataGrid.Items.Refresh();
        }

        /// <summary>
        /// 加载绑定数据集合
        /// </summary>
        /// <param name="selector">带选择项的控件</param>
        /// <param name="itemsSource">数据集合</param>
        public static void LoadItemsSource(Selector selector, IEnumerable itemsSource)
        {
            selector.ItemsSource = itemsSource;
            if (selector.HasItems)
            {
                selector.SelectedIndex = 0;
            }
            selector.Items.Refresh();
        }

        public static void AppendToItemsSource<T>(Selector selector, T item) where T : class
        {
            IEnumerable itemsSource = selector.ItemsSource;
            List<T> itemsList = (List<T>)itemsSource;
            itemsList.Add(item);

            LoadItemsSource(selector, itemsList);
            selector.SelectedItem = item;
        }

        /// <summary>
        /// 加载绑定数据集合
        /// </summary>
        /// <param name="selector">带选择项的控件</param>
        /// <param name="itemsSource">数据数组</param>
        public static void LoadItemsSource(Selector selector, Array itemsSource)
        {
            selector.ItemsSource = itemsSource;
            if (selector.HasItems)
            {
                selector.SelectedIndex = 0;
            }
            selector.Items.Refresh();
        }

        /// <summary>
        /// 卸载数据表格数据
        /// </summary>
        /// <param name="dataGrid">数据表格</param>
        public static void UnLoadItemsSource(DataGrid dataGrid)
        {
            dataGrid.IsReadOnly = false;
            dataGrid.ItemsSource = null;
            dataGrid.IsReadOnly = true;
        }

        /// <summary>
        /// 加载选中项
        /// </summary>
        /// <param name="dataGrid">DataGrid</param>
        /// <param name="item">选中项</param>
        public static void LoadSelectedItem(DataGrid dataGrid, object item)
        {
            dataGrid.IsReadOnly = false;
            dataGrid.SelectedItem = item;
            dataGrid.IsReadOnly = true;
        }

        /// <summary>
        /// 更新到项列表
        /// </summary>
        /// <typeparam name="T">项类型</typeparam>
        /// <param name="items">项列表</param>
        /// <param name="propertyName">项主键属性名称</param>
        /// <param name="item">更新项</param>
        public static void UpdateToItems<T>(List<T> items, string propertyName, T item) where T : class
        {
            items.Find(delegate(T __item)
            {
                object __itemValue = ReflectionUtil.GetPropertyValue(__item, propertyName);
                object itemValue = ReflectionUtil.GetPropertyValue(item, propertyName);

                if (itemValue == __itemValue)
                {
                    __item = item;
                    return true;
                }
                else
                {
                    return false;
                }
            });
        }

        /// <summary>
        /// 创建改变量
        /// </summary>
        /// <param name="entityType">实体类类型</param>
        /// <returns></returns>
        public static T CreateDelta<T>(Type entityType) where T : EntityDeltaBase, new()
        {
            T delta = new T();

            delta.EntityName = entityType.Name;
            delta.Conditions = new Dictionary<string, object>();
            delta.DeltaProperties = new Dictionary<string, object>();

            return delta;
        }

        /// <summary>
        /// 创建改变量
        /// </summary>
        /// <param name="entityType">实体类类型</param>
        /// <returns></returns>
        public static EntityDeltaBase CreateDelta(Type entityType)
        {
            var delta = new EntityDeltaBase();

            delta.EntityName = entityType.Name;
            delta.Conditions = new Dictionary<string, object>();
            delta.DeltaProperties = new Dictionary<string, object>();

            return delta;
        }

        /// <summary>
        /// 获取改变量
        /// </summary>
        /// <param name="oldObj">原对象</param>
        /// <param name="newObj">现对象</param>
        /// <returns></returns>
        public static EntityDeltaBase GetDelta(object oldObj, object newObj)
        {
            var objType = oldObj.GetType();
            var delta = CreateDelta(objType);
            var properties = ReflectionUtil.GetProperties(objType, typeof(DBFieldAttribute));

            foreach (var property in properties)
            {
                string propertyName = property.Name;
                object oldValue = ReflectionUtil.GetPropertyValue(oldObj, propertyName);
                object newValue = ReflectionUtil.GetPropertyValue(newObj, propertyName);

                bool isEquals = false;

                if (oldValue != null && newValue != null)
                {
                    isEquals = oldValue.Equals(newValue);
                }
                else if (oldValue == null && newValue == null)
                {
                    isEquals = true;
                }

                if (!isEquals)
                {
                    delta.DeltaProperties.Add(propertyName, newValue);
                }
            }

            return delta;
        }

        /// <summary>
        /// 实体转换器
        /// </summary>
        /// <param name="sourceEntity"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object ConvertEntity(object sourceEntity, Type type)
        {
            object targetEntity = ReflectionUtil.CreateInstance(type);

            PropertyInfo[] propertyArray = ReflectionUtil.GetProperties(targetEntity);
            foreach (var property in propertyArray)
            {
                string propertyName = property.Name;
                object propertyValue = ReflectionUtil.GetPropertyValue(sourceEntity, propertyName);

                if ((propertyValue is IEnumerable) && !(propertyValue is string))
                {
                    continue;
                }

                if (propertyValue is EntityBase)
                {
                    PropertyInfo __property = ReflectionUtil.GetProperty(targetEntity, propertyName);
                    if (__property.PropertyType != propertyValue.GetType())
                    {
                        Type __type = __property.PropertyType;
                        propertyValue = ConvertEntity(propertyValue, __type);
                    }
                }

                ReflectionUtil.SetPropertyValue(targetEntity, propertyName, propertyValue);
            }

            return targetEntity;
        }

        /// <summary>
        /// 创建打开文件对话框
        /// </summary>
        /// <param name="title">对话框标题</param>
        /// <param name="filter">筛选字符串，如：所有文件(*.*)|*.*</param>
        /// <returns></returns>
        public static OpenFileDialog CreateOpenFileDialog(string title, string filter)
        {
            var dialog = new OpenFileDialog();
            dialog.Title = title;
            dialog.Filter = filter;
            dialog.InitialDirectory = ApplicationUtil.GetBaseDirectory();
            dialog.RestoreDirectory = true;
            dialog.Multiselect = false;

            return dialog;
        }
        #endregion
    }
}
